package com.example.userdemo.controller;

import com.example.userdemo.model.User;
import com.example.userdemo.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public ResponseEntity<Page<User>> getAllUsers(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer age,
            @RequestParam(required = false) String occupation,
            @RequestParam(required = false) Date birthday,
            @RequestParam(required = false) String gender,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (gender != null) {
            wrapper.like("gender", gender);
        }
        if (name != null) {
            wrapper.like("name", name);
        }
        if (age != null) {
            wrapper.eq("age", age);
        }
        if (occupation != null) {
            wrapper.like("occupation", occupation);
        }
        if (birthday != null) {
            wrapper.eq("birthday", birthday);
        }
        Page<User> users = userService.getAllUsers(new Page<>(page, pageSize), wrapper);
        return new ResponseEntity<>(users, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable(value = "id") Long userId) {
        // 假设 userService.getUserById 返回的是 User 类型，将其包装成 Optional<User>
        User userObj = userService.getUserById(userId);
        Optional<User> user = Optional.ofNullable(userObj);
        return user.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        // 确保性别属性被正确设置
        if (user.getGender() == null) {
            user.setGender("未知");
        }
        User createdUser = userService.createUser(user);
        return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<User> updateUser(@PathVariable(value = "id") Long userId,
                                           @RequestBody User userDetails) {
        // 确保性别属性被正确更新
        if (userDetails.getGender() == null) {
            userDetails.setGender("未知");
        }
        User updatedUser = userService.updateUser(userDetails);
        return new ResponseEntity<>(updatedUser, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable(value = "id") Long userId) {
        userService.deleteUser(userId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
